/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.util;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import nl.cloudfarming.client.logging.AppLogFactory;
import nl.cloudfarming.client.logging.AppLogger;
import org.geotools.geometry.jts.JTSFactoryFinder;

/**
 *
 * @author Gerben Feenstra
 */
public class GeometryUtil {
  
    private GeometryUtil(){}
    
    private static final AppLogger LOG = AppLogFactory.getLogger(GeometryUtil.class);

    // DO NOT USE -> USE getGeometryFactory()!!
    private static GeometryFactory geometryFactory;
 
    private static final double ACREAGE_DECIMALS = 100.0d;
    private static final double ACREAGE_MULTIPLIER = 1000000d;
    
    /*
     * Uses JTSFactoryFinder to find the GeometryAfctory
     */
    private static GeometryFactory getGeometryFactory(){
        if(geometryFactory != null){
            return geometryFactory;
        }
        else{
            return (geometryFactory = JTSFactoryFinder.getGeometryFactory(null));
        }
    }
    
    /**
     * Returns a geometry made from the specified wkt String
     * @param wkt The wkt String
     * @return The Geometry extracted from the String
     */
    public static Geometry wktToGeometry(String wkt){
        try {
            
            WKTReader wKTReader = new WKTReader(getGeometryFactory());
            return wKTReader.read(wkt);
        } catch (ParseException ex) {
            String error = "Failed converting WKT to Geometry";
            LOG.error(error,ex);
            throw new WktToGeometryConversionException(error, ex);
        }
    }
    
    /**
     * Create a Point from a longitude and a latitude value using the specified geometryFactory
     * @param geometryFactory The geometryfactory to use
     * @param longitude 
     * @param latitude
     * @return Point created from the specified coordinates
     */
    public static Point getPoint(GeometryFactory geometryFactory, double longitude, double latitude){
        Coordinate coordinate = new Coordinate(longitude, latitude);
        Point geometry = geometryFactory.createPoint(coordinate);
        return geometry;
    }
    
    /**
     * Convenience method, retrieves a GeometryFactory from the JTSFactoryFinder using getGeometryFactory and calls getPoint(GeometryFactory geometryFactory, double longitude, double latitude)
     * @param longitude 
     * @param latitude
     * @return Point created from the specified coordinates
     */
    public static Point getPoint(double longitude, double latitude){
        return getPoint(getGeometryFactory(), longitude, latitude);
    }    
    

    /**
     * Return acreage in Hectares
     * 
     * @param acreage as provided by vividsolutions Shape
     * @return Acreage in HA
     */
    public static Double getAcreageInHA(Double acreage){
//        return acreage > 0 ? Math.round((acreage * ACREAGE_MULTIPLIER * ACREAGE_DECIMALS)) / ACREAGE_DECIMALS : acreage;
        return acreage > 0 ? acreage * ACREAGE_MULTIPLIER : acreage;        
    }

}
